
package com.neegix.organization.psn.interfaces.controller;

import com.neegix.base.PageVO;
import com.neegix.exception.BusinessRuntimeException;
import com.neegix.result.Result;
import com.neegix.organization.psn.application.assembler.PsnAssembler;
import com.neegix.organization.psn.application.cqrs.query.PsnQueryRepository;
import com.neegix.organization.psn.domain.entity.PsnEntity;
import com.neegix.organization.psn.interfaces.vo.PsnVO;
import com.neegix.organization.psn.application.service.PsnService;
import com.neegix.organization.psn.interfaces.form.NewPsnForm;
import com.neegix.organization.psn.interfaces.form.QueryPsnForm;
import com.neegix.organization.psn.interfaces.form.UpdatePsnForm;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-11 15:13:32
  */

@RestController
@RequestMapping("/api/v1/psn")
public class PsnController {

    @Autowired
    private PsnQueryRepository psnQueryRepository;

    @Autowired
    private PsnService psnService;

    @PreAuthorize("hasAuthority('organization:psn:add')")
    @PostMapping
    public Result<Void> createPsn(@RequestBody @Valid NewPsnForm psnForm){
        Void result = psnService.createPsn(PsnAssembler.INSTANCE.covertEntity(psnForm));
        return Result.success("创建成功", result);
    }

    @PreAuthorize("hasAuthority('organization:psn:modify')")
    @PutMapping("/{id}")
    public Result<Void> updatePsn(@PathVariable Long id, @RequestBody @Valid UpdatePsnForm psnForm){
        PsnEntity psnEntity = PsnAssembler.INSTANCE.covertEntity(psnForm);
        psnEntity.setId(id);
        Void result = psnService.modifyPsn(psnEntity);
        return Result.success("更新成功",result);
    }

    @PreAuthorize("hasAuthority('organization:psn:list')")
    @GetMapping({"/{currentPage}/{pageSize}/{pkBizUnit}","/{currentPage}/{pageSize}/{pkBizUnit}/{pkDept}"})
    public Result<PageVO<PsnVO>> getPsns(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize,@PathVariable("pkBizUnit") Long pkBizUnit, @PathVariable(name = "pkDept", required=false) Long pkDept, @ModelAttribute QueryPsnForm psnForm){
        if(psnForm == null) {
            psnForm = new QueryPsnForm();
        }
        PageVO<PsnVO> pageVO = psnQueryRepository.findPage(currentPage, pageSize, pkBizUnit, pkDept, PsnAssembler.INSTANCE.covertDTO(psnForm));
        return Result.success("查询成功",pageVO);
    }

    @PreAuthorize("hasAuthority('organization:psn:get')")
    @GetMapping("/{id}")
    public Result<PsnVO> getPsnById(@PathVariable("id") Long id) {
        Optional<PsnVO> optional = psnQueryRepository.findById(id);
        return Result.success("获取成功", optional.orElseThrow(()-> new BusinessRuntimeException("查询结果不存在")));
    }

    @PreAuthorize("hasAuthority('organization:psn:remove')")
    @DeleteMapping
    public Result<Void> removePsn(@RequestBody List<Long> ids){
        return Result.success("删除成功", psnService.removePsn(ids));
    }
}